package admin

import (
	"yunj/pkg/global"
	"yunj/pkg/yunj/app/enum/admin/authenum"
	"yunj/pkg/yunj/app/service/admin/adminservice"
	"yunj/pkg/yunj/app/service/admin/authservice"
	errcodeEnum "yunj/pkg/yunj/core/enum/errcode"
	"yunj/pkg/yunj/util"

	"github.com/gin-gonic/gin"
)

type Index struct {
	Controller
}

// 首页
func (ctrl *Index) Index(c *gin.Context) {
	adminMember := adminservice.GetLoginMember(c)
	memberDesc := adminMember.Name
	if memberDesc == "" {
		memberDesc = adminMember.Username
	}
	sidebarMenuHtml, err := authservice.Auth.GetMenuHtml(c, authenum.TypeByValue(authenum.TYPE_SIDEBAR_MENU))
	if err != nil {
		util.RedirectAdminTips(c, errcodeEnum.ERROR, err.Error())
	}
	topMenuHtml, err := authservice.Auth.GetMenuHtml(c, authenum.TypeByValue(authenum.TYPE_TOP_MENU))
	if err != nil {
		util.RedirectAdminTips(c, errcodeEnum.ERROR, err.Error())
	}
	globalSearchResultHtml, err := authservice.Auth.GetGlobalSearchResultHtml(c)
	if err != nil {
		util.RedirectAdminTips(c, errcodeEnum.ERROR, err.Error())
	}

	data := map[string]any{
		"memberId":               adminMember.Id,
		"memberDesc":             memberDesc,
		"memberInfoUrl":          util.BuildAdminUrl("/member/info", map[string]any{"id": adminMember.Id}),
		"logoutUrl":              global.Config.GetAppAdminEntrance() + "/logout",
		"dashboardUrl":           global.Config.App.Admin.DashboardUrl,
		"isMobile":               util.IsMobile(c),
		"sidebarMenuHtml":        sidebarMenuHtml,
		"topMenuHtml":            topMenuHtml,
		"globalSearchResultHtml": globalSearchResultHtml,
	}

	ctrl.Render(c, "index/index", data)
}
